/**
 */
package com.dingpeikeji.quickdeer.core.modules.orders.web;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alipay.api.RefundFastpayByPlatformNopwdApi;
import com.alipay.model.RefundFastpayByPlatformNopwdReq;
import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.utils.excel.ExportExcel;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.cupons.entity.CuponsInfo;
import com.dingpeikeji.quickdeer.core.modules.cupons.service.TCuponsService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrdersOut;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersOutService;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.core.modules.payinfo.entity.TPayinfo;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealsche;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealscheVehi;
import com.dingpeikeji.quickdeer.core.modules.realsche.service.TRealscheService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.modules.vehi.service.TRealscheVehisService;
import com.dingpeikeji.quickdeer.mobile.service.OrderService;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProRentOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TDemandsService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.sms.entity.Sms;
import com.dingpeikeji.quickdeer.modules.sms.service.SmsApi;
import com.dingpeikeji.quickdeer.modules.wx.realschevehi.service.WxRealscheVehiService;
import com.google.common.collect.Lists;
import com.tencent.WXPay;
import com.tencent.common.Configure;
import com.tencent.protocol.refund_protocol.RefundReqData;

/**
 * 线下订单
 * 
 * @author lh
 * @version 2016-06-15
 */
@Controller
@RequestMapping(value = "${adminPath}/offline/tOffline")
public class TOrdersOfflineController extends BaseController {

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
	SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	private TOrdersService tOrdersService;

	@Autowired
	private TCargotypeService tCargotypeService;

	@Autowired
	private RentproductOrderService rentproductOrderService;
	
	@Autowired
	private TRealscheService tRealscheService;
	
	@Autowired
	private TRentgroupService tRentgroupService;
	
	@Autowired
	private TRentproductService tRentproductService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private WxRealscheVehiService wxRealscheVehiService;
	
	@ModelAttribute
	public TOrders get(@RequestParam(required = false) String id) {
		TOrders entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = tOrdersService.get(id);
		}
		if (entity == null) {
			entity = new TOrders();
		}
		return entity;
	}

	
	@RequestMapping(value = { "form", "" })
	public String list(TOrders tOrders, HttpServletRequest request, HttpServletResponse response, Model model) {
		// 车型数据
		List<TCargotype> cargotypeList = tCargotypeService.findList(new TCargotype());
		model.addAttribute("cargotypeList", cargotypeList);
		return "modules/offline/tOfflineForm";
	}
	
	@RequestMapping(value = "save")
	@Transactional
	public String save(HttpServletRequest request,TOrders tOrders, Model model, RedirectAttributes redirectAttributes) throws Exception {
		tOrders.setOrdertype("7");
		tOrders.setOrderstatus("0");
		tOrders.setRealboardid(request.getParameter("departcity.siteid"));
		tOrders.setRealoffid(request.getParameter("destcity.siteid"));
		tOrders.setDepartcity(request.getParameter("departcity.sitename"));
		tOrders.setDestcity(request.getParameter("destcity.sitename"));
		tOrders.setDepartdateStr(sdf.format(tOrders.getDepartdate()));
		tOrders.setOrdername(tOrders.getDepartcity()+"-"+tOrders.getDestcity()+tOrders.getBookingdays()+"日包车");
		tOrders.setOrderuserid(UserUtils.getUser().getId());
		tOrders.setOffice(UserUtils.getUser().getOffice());
		tOrders.setOfficeids(UserUtils.getUser().getOffice().getId());
		tOrders.setDelFlag("0");
		tOrders.setIscheck("N");
		tOrders.setUseCupons("0");
		tOrders.setPayway("1");
		tOrders.setSettlement("0");
//		if(tOrders.getOrderRemarks().equals("其他")){
//			if(request.getParameter("orderRemarks2").length()>0){
//				tOrders.setOrderRemarks(request.getParameter("orderRemarks2"));
//			}
//		}
		TCargotype car =tCargotypeService.get(tOrders.getCartype().getCargotypeid()+"");
		tOrders.setCartype(car);
		
//		TRentgroup tRentgroup =new TRentgroup();
//		tRentgroup.setOffice(UserUtils.getUser().getOffice());
//		tRentgroup.setProductname(tOrders.getOrdername());
//		tRentgroup.setDaysnum(tOrders.getBookingdays());
//		tRentgroup.setRenttype("4");
//		tRentgroup.setAuditstatus("1");
//		tRentgroup.setRange("2");
//		TSitedtl depart =new TSitedtl();
//		depart.setSitename(request.getParameter("departcity.sitename"));
//		depart.setSiteid(request.getParameter("departcity.siteid"));
//		TSitedtl dest =new TSitedtl();
//		dest.setSitename(request.getParameter("destcity.sitename"));
//		dest.setSiteid(request.getParameter("destcity.siteid"));
//		tRentgroup.setDepartcity(depart);
//		tRentgroup.setDestcity(dest);
//		tRentgroup.setCreateBy(UserUtils.getUser());
//		tRentgroup.setDelFlag("0");
//		tRentgroupService.insertOffline(tRentgroup);
		
//		TRentproduct tRentproduct =new TRentproduct();
//		//tRentproduct.setRentgrpid(tRentgroup.getRentgrpid());
//		tRentproduct.setDepartcity(depart);
//		tRentproduct.setDestcity(dest);
//		tRentproduct.setOffice(UserUtils.getUser().getOffice());
//		tRentproduct.setRentType("4");
//		tRentproduct.setCargotype(car);
//		tRentproduct.setSeats(car.getSeats());
//		tRentproduct.setPacks(car.getPacks());
//		tRentproduct.setSuitrtnprice(0.0);
//		tRentproductService.save(tRentproduct);
		
		TRealsche tRealsche =new TRealsche();
		//tRealsche.setScheid(tRentproduct.getRentid()+"");
		tRealsche.setSchename(tOrders.getOrdername());
		tRealsche.setDepartdate(sd.parse(tOrders.getDepartdateStr()));
		tRealsche.setVehiclenum(tOrders.getPurchasenum());
		tRealsche.setOfficeid(tOrders.getOfficeids());
		tRealsche.setCompanyid(tOrders.getOfficeids());
		tRealsche.setCreateby(tOrders.getOrderuserid());
		tRealsche.setDispstatus("1");
		tRealsche.setModeldesc(tOrders.getOrderRemarks());
		tRealsche.setRemarks(tOrders.getRemarks());
		tRealsche.setSchekind("4");
		tRealscheService.insertOffline(tRealsche);
		
		TRealscheVehi tRealscheVehi =new TRealscheVehi();
		tRealscheVehi.setRealscheid(tRealsche.getRealscheid());
		tRealscheVehi.setOffice(UserUtils.getUser().getOffice());
		tRealscheVehi.setBusid(0);
		tRealscheVehi.setCargotypeid(tOrders.getCartype().getCargotypeid());
		tRealscheVehi.setSeats("0");
		tRealscheVehi.setMaindriver("0");;
		tRealscheVehi.setDeparttime(tOrders.getDepartdate());
		tRealscheVehi.setDuringdays(tOrders.getBookingdays()+"");
		tRealscheVehi.setSchekind("4");
		tRealscheVehi.setVehistatus("1");
		wxRealscheVehiService.insertOffline(tRealscheVehi);
		
		tOrders.setRealscheid(tRealsche.getRealscheid());
		tOrdersService.save(tOrders);
		if(tOrders.getOrderid()!=null){
			orderService.updateOffline(tOrders);
			redirectAttributes.addFlashAttribute("message", "订单添加成功");
			return "redirect:form";
		}else{
			redirectAttributes.addFlashAttribute("message", "订单添加失败");
			return "redirect:form";
		}
//		ProRentOrders pro = null;
//		try {
//				pro = rentproductOrderService.genOrder(tOrders);
//				@SuppressWarnings("unused")
//				boolean success = pro.success();
//				if (pro.success()) { // 如果成功生成订单，跳到支付方式选择页面
//					Integer orderid = pro.getOrderid();
//					orderService.updateOffline(orderid+"");
//					redirectAttributes.addFlashAttribute("message", "订单添加成功");
//					return "redirect:form";
//				}
//			} catch (Exception e) {
//				e.printStackTrace();
//				redirectAttributes.addFlashAttribute("message", "订单添加失败");
//				return "redirect:form";
//			}
	}
	

}
